package com.person.android.utils;

import com.person.android.dao.FormatType;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @autor zhaokang.
 * @time on 2015/12/10.
 */
public class FormatTypeUtils {

    private static List<String> getAudioFormats() {
        List<String> audioFormats = new ArrayList<>();
        audioFormats.add("mp3");
        audioFormats.add("wav");
        audioFormats.add("aac");
        audioFormats.add("amr");
        audioFormats.add("mid");
        audioFormats.add("wma");
        audioFormats.add("flac");
        return audioFormats;
    }

    private static List<String> getVieoFormats() {
        List<String> audioFormats = new ArrayList<>();
        audioFormats.add("mp4");
        audioFormats.add("flv");
        audioFormats.add("mkv");
        audioFormats.add("rmvb");
        audioFormats.add("avi");
        audioFormats.add("3gp");
        audioFormats.add("mov");
        audioFormats.add("rm");
        audioFormats.add("asf");
        return audioFormats;
    }

    private static List<String> getTextFormats() {
        List<String> audioFormats = new ArrayList<>();
        audioFormats.add("txt");
        audioFormats.add("pdf");
        audioFormats.add("html");
        audioFormats.add("xml");
        audioFormats.add("doc");
        audioFormats.add("ppt");
        audioFormats.add("xls");
        return audioFormats;
    }

    private static List<String> getPictureFormats() {
        List<String> formats = new ArrayList<>();
        formats.add("png");
        formats.add("jpg");
        formats.add("gif");
        formats.add("bmp");
        formats.add("psd");
        return formats;
    }

    public static boolean isAudioFormat(String format) {
        boolean result = false;
        List<String> formats = getAudioFormats();
        int size = formats.size();
        for (int i = 0; i < size; i++) {
            if (format.equals(formats.get(i))) {
                result = true;
                break;
            }
        }
        return result;
    }

    public static boolean isTxtFormat(String format) {
        List<String> formats = getTextFormats();
        int size = formats.size();
        for (int i = 0; i < size; i++) {
            if (format.contains(formats.get(i))) {
                return true;
            }
        }
        return false;
    }

    public static boolean isPictureFormat(String format) {
        List<String> formats = getPictureFormats();
        int size = formats.size();
        for (int i = 0; i < size; i++) {
            if (format.equals(formats.get(i))) {
                return true;
            }
        }
        return false;
    }

    public static boolean isVideoFormat(String format) {
        List<String> formats = getVieoFormats();
        int size = formats.size();
        for (int i = 0; i < size; i++) {
            if (format.equals(formats.get(i))) {
                return true;
            }
        }
        return false;
    }

    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    public static int countValidSubFilesNum(String filePath) {
        int folderCount = 0;
        int audioCount = 0;
        File[] files = new File(filePath).listFiles();
        int number = files.length;
        for (int i = 0; i < number; i++) {
            if (files[i].isDirectory()) {
                folderCount++;
            } else if (isAudioFormat(getExtensionName(files[i].getPath()))) {
                audioCount++;
            }
        }
//        LogUtils.logInfo(LogUtils.FileListActivityFlag, filePath + " contains the (folder,audio):=(" + folderCount + "," + audioCount + ")");
        return folderCount + audioCount;
    }

    /**
     * 根据输入格式获取对应的转换格式
     *
     * @param fmt 选择的输入格式
     * @return 返回支持的对应的格式列表
     */
    public static List<String> getAudioTransformFmt2(String fmt) {
        List<String> result = new ArrayList<>();
        if (fmt.equals("mp3")) {
            result = getAudioFormats();
            result.remove("mp3");
        } else if (fmt.equals("wav")) {
            result = getAudioFormats();
            result.remove("wav");
        } else if (fmt.equals("flac")) {
            result = getAudioFormats();
            result.remove("flac");
        } else if (fmt.equals("aac")) {
            result = getAudioFormats();
            result.remove("aac");
        } else if (fmt.equals("wma")) {
            result = getAudioFormats();
            result.remove("wma");
        }
        return result;
    }

    /**
     * 根据输入格式获取对应的转换格式
     *
     * @param fmt 选择的输入格式
     * @return 返回支持的对应的格式列表
     */
    public static List<String> getSupportAudioFormat(String fmt) {
        List<String> result = new ArrayList<>();
        if ("mp3".equals(fmt)) {
            result.add("wav");
        } else if ("wav".equals(fmt)) {
            result.add("mp3");
            result.add("pcm");
        } else if ("aac".equals(fmt)) {

        }
        return result;
    }

    /**
     * 判断是否是支持的音频格式
     *
     * @param input  选择的输入格式
     * @param output 选择的输出格式
     * @return 如果支持返回true，否则返回false
     */
    public static boolean isSupportAudioFormat(String input, String output) {
        boolean result = false;
        if (input.equals("mp3") && output.equals("wav")) {
            result = true;
        } else if (input.equals("wav") && (output.equals("mp3") || output.equals("pcm"))) {
            result = true;
        }
        return result;
    }

    public static int getFormat(String input, String output) {
        int type = 0;
        if (input.equals("mp3") && output.equals("wav")) {
            type = FormatType.MP3_WAV;
        } else if (input.equals("wav") && output.equals("mp3")) {
            type = FormatType.WAV_MP3;
        } else if (input.equals("wav") && output.equals("pcm")) {
            type = FormatType.WAV_PCM;
        }
        return type;
    }

}
